package com.k.base.jdk.innerclass;

public class Outer {
    private String outerUser = "outerFieldValue";

    public String getOuterUser() {
        return outerUser;
    }

    public void setOuterUser(String outerUser) {
        this.outerUser = outerUser;
    }

    /**
     * 成员内部类
     * [修饰符] class className{}
     */
    private class Inner{
        private String innerUser = "innerUser";

        public String getInnerUser() {
            return innerUser;
        }
        public void setInnerUser(String innerUser) {
            this.innerUser = innerUser;
        }
        public String testInner(){
            return "private inner class: testInner 方法 innerField 属性:"+innerUser + " outerField:"+outerUser;
        }
    }

    /**
     * 成员内部类
     * [修饰符] class className{}
     */
    public class InnerPublic{
        private String innerPublicUser = "innerPublicUser";

        public String getInnerPublicUser() {
            return innerPublicUser;
        }
        public void setInnerPublicUser(String innerPublicUser) {
            this.innerPublicUser = innerPublicUser;
        }
        public String testInnerPublic(){
            return "public inner class: testInner 方法 innerField 属性:"+innerPublicUser + " outerField:"+outerUser;
        }
    }

    /**
     * 静态内部类
     * [修饰符] static class className{}
     */
    static class InnerStatic{
        private String innerStaticUser = "innerStaticUser";
        private static String innerStaticFieldStatic="InnerStaticFieldStatic";

        public String getInnerStaticUser() {
            return innerStaticUser;
        }
        public void setInnerStaticUser(String innerStaticUser) {
            this.innerStaticUser = innerStaticUser;
        }
        public void testInnerStatic() {
            System.out.println("testInner innerField:"+innerStaticUser);
        }
    }

    /**
     * 内部类一般用于类中使用的类，不用再新建一个新类
     * 外部方法测试内部类
     */
    public void test(){
        System.out.println("成员内部类: private");
        Inner inner = new Inner();
        System.out.println("outer invoke: " + "private inner class: privateField:"+inner.getInnerUser());
        System.out.println("outer invoke: " + inner.testInner());

        System.out.println("成员内部类: public");
        InnerPublic innerPublic = new InnerPublic();
        System.out.println("outer invoke: " + "public inner class: privateField:"+innerPublic.getInnerPublicUser());
        System.out.println("outer invoke: " + innerPublic.testInnerPublic());

        System.out.println("静态内部类:");
        InnerStatic innerStatic = new InnerStatic();
        System.out.println("outer invoke: " + "inner static class: privateField:"+innerStatic.getInnerStaticUser());
        System.out.println("outer invoke: " + "inner static class: privateStaticField:"+innerStatic.innerStaticFieldStatic);

        // 局部内部类 定义在一个方法里，和局部变量 定义的位置相同
        // 局部内部类 [final/abstract] class 不能有修饰符
        class Inner3{
            private String inner3Field = "inner3FieldValue";
            public String getInner3Field() {
                return inner3Field;
            }
            public void setInner3Field(String inner3Field) {
                this.inner3Field = inner3Field;
            }
        }
        Inner3 inner3 = new Inner3();
        System.out.println("局部内部类: "+inner3.getInner3Field());

        // 匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("run");
            }
        }).start();
    }
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.test();
    }
}
